﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;

namespace NLBS.Data
{
    public partial class AccessDataProvider : DbProvider
    {

        #region WordFilter

        public override IList<WordFilter> GetWordFilter()
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT wf_id, wf_mode, wf_text, wf_replace, wf_regExp FROM blog_WordFilter");

            IList<WordFilter> lists = new List<WordFilter>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    WordFilter wf = CommonDataProvider.PopulateWrodFilterFromIDataReader(dataReader);
                    lists.Add(wf);
                }
            }
            return lists;
        }

        public override WordFilter GetWordFilterByID(int itemID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT   wf_id, wf_mode, wf_text, wf_replace, wf_regExp FROM blog_WordFilter WHERE (wf_id = ?)");
            db.AddInParameter(dbCommand, "itemID", DbType.Int32, itemID);
            WordFilter wf = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    wf = CommonDataProvider.PopulateWrodFilterFromIDataReader(dataReader);
                }
            }
            return wf;
        }

        public override int InsertWordFilter(WordFilter item)
        {
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();
                try
                {
                    DbCommand dbCommand = db.GetSqlStringCommand("INSERT INTO blog_WordFilter (wf_mode, wf_text, wf_replace, wf_regExp) VALUES   (?, ?, ?, ?)");
                    db.AddInParameter(dbCommand, "mode", DbType.Byte, item.Mode);
                    db.AddInParameter(dbCommand, "text", DbType.String, item.Text);
                    db.AddInParameter(dbCommand, "replace", DbType.String, item.Replace);
                    db.AddInParameter(dbCommand, "regExp", DbType.Boolean, item.RegExp);
                    db.ExecuteNonQuery(dbCommand, transaction);
                    dbCommand.CommandText = "SELECT @@IDENTITY";
                    int result = (int)db.ExecuteScalar(dbCommand, transaction);
                    transaction.Commit();
                    return result;
                }
                catch
                {
                    // Roll back the transaction. 
                    transaction.Rollback();
                }
                connection.Close();
            }
            return 0;
        }

        public override void UpdateWordFilter(WordFilter item)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("UPDATE `blog_WordFilter` SET `wf_mode` = ?, `wf_text` = ?, `wf_replace` = ?, `wf_regExp` = ? WHERE ((`wf_id` = ?))");
            db.AddInParameter(dbCommand, "id", DbType.String, item.WordFilterID);
            db.AddInParameter(dbCommand, "mode", DbType.Byte, item.Mode);
            db.AddInParameter(dbCommand, "text", DbType.String, item.Text);
            db.AddInParameter(dbCommand, "replace", DbType.String, item.Replace);
            db.AddInParameter(dbCommand, "regExp", DbType.Boolean, item.RegExp);
            db.ExecuteNonQuery(dbCommand);
        }

        public override void DeleteWordFilter(int itemID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM `blog_WordFilter` WHERE ((`wf_id` = ?))");
            db.AddInParameter(dbCommand, "id", DbType.String, itemID);
            db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
